"""\
GameOverMenuFrame is a class provide a frame with a rank list display and some buttons.
It includes two OnScreenText:
     txt_Pass to show game pass or fail
     txt_Rank to show rank list

and two buttons:
    btn_MainMenu for "Back to Menu" -> messenger.send(event_GAME_BACKTOMAINMENU)
    btn_Restart for "Restart" -> messenger.send(event_GAME_GAMELOAD)

and driven state changed in GameFSM

GameOverMenuFrame.__init__(self)
    Create a GameOverMenuFrame.
"""
# event
from Shell.data.GameEventDefine import event_GAME_BACKTOMAINMENU, event_GAME_GAMELOAD
# gui
from direct.gui.DirectGui import DirectFrame, DirectButton, OnscreenText
from pandac.PandaModules import TextNode
from Cube.gui.DirectGUIPropertiesAccess import BtnTextSet
# uiString
from Shell.data.UIString import uiString
# python object save/read
from Cube.utility.PythonObjectFileAccess import ObjectLoadFromFile, ObjectSaveToFile
# Game Over Info
import Shell.data.GameOverInfo as GameOverInfo

class GameOverMenuFrame(DirectFrame):
    filepath_rank = "rank.save"
    def __init__(self):
        """\
GameOverMenuFrame.__init__(self)
    Create a GameOverMenuFrame."""
        DirectFrame.__init__(self)

        IsPass = GameOverInfo.Info["pass"]
        EscapedTime = GameOverInfo.Info["escaped time"]

        frame_Board = DirectFrame(parent=self,
                                  frameColor=(0.4,0.4,0.4,0.9),
                                  frameSize=(-0.9,0.9,-0.9,0.9))

        scale=0.08
        btnFrameSize = (-4,4,-0.5,1)
        txt_Pass = OnscreenText(parent=self,
                                scale = 0.12,
                                pos = (0,0.7),
                                fg = (1,1,1,1))
        txt_Rank = OnscreenText(parent=self,
                                scale = 0.1,
                                pos = (-0.2,0.5),
                                fg = (1,1,1,1),
                                align = TextNode.ALeft)
        btn_MainMenu = DirectButton(parent=self,
                                    scale=scale,
                                    pos =(-0.5,0,-0.8),
                                    frameSize = btnFrameSize,
                                    command= messenger.send,
                                    extraArgs = [event_GAME_BACKTOMAINMENU]
                                    )
        btn_Restart = DirectButton(parent=self,
                                   scale=scale,
                                   pos =(0.5,0,-0.8),
                                   frameSize = btnFrameSize,
                                   command= messenger.send,
                                   extraArgs = [event_GAME_GAMELOAD]
                                   )


        txt_Pass.setText("%s"%("%s - %.1f"%(uiString["Clear"],EscapedTime) if IsPass else uiString["Game Over"]))
        rankList = self.RankGet(EscapedTime if IsPass else None)[:10]
        txt_Rank.setText("\n".join(
                        ["%d: %.1f %s"%(i+1, rec, uiString["sec"])
                         for (i,rec) in zip(range(len(rankList)) ,rankList) ]))
        BtnTextSet(btn_MainMenu, uiString["Back to Menu"])
        BtnTextSet(btn_Restart, uiString["Restart"])

        if rankList: self.RankSave(rankList)

    def RankGet(self, newRecord = None):
        try:
            rankList = ObjectLoadFromFile(self.filepath_rank)
        except: rankList = []
        if newRecord: rankList.append(newRecord)
        rankList.sort()
        return rankList

    def RankSave(self, rankList):
        ObjectSaveToFile(self.filepath_rank, rankList)
